var pandaBoxParseList = [
    { "label": "关闭线路", "value": "" },
    { "label": "线路1", "value": "https://jx.playerjy.com/?ads=0&url=" },
    { "label": "线路2", "value": "https://z1.m1907.top/?jx=" },
    { "label": "线路3", "value": "https://jx.xmflv.cc/?url=" },
    { "label": "线路4", "value": "https://yparse.ik9.cc/index.php?url=" },
    { "label": "线路5", "value": "https://www.pangujiexi.com/jiexi/?url=" },
    { "label": "线路6", "value": "https://www.mtosz.com/erzi.php?url=" },
    { "label": "线路7", "value": "https://yparse.jn1.cc/index.php?url=" },
    { "label": "线路8", "value": "https://video.isyour.love/player/getplayer?&amp;adadress=https://diaoyong/dy.html?&amp;url=" },
    { "label": "线路9", "value": "https://api.quanminjiexi.com/?v=" },
    { "label": "线路10", "value": "https://www.8090g.cn/?url=" },
    { "label": "线路11", "value": "https://www.playm3u8.cn/jiexi.php?url=" },
    { "label": "线路12", "value": "https://www.daga.cc/vip2/?url=" },
    { "label": "线路13", "value": "https://www.daga.cc/vip3/?url=" },
    { "label": "线路14", "value": "https://jx.m3u8.tv/jiexi/?url=" },
    { "label": "线路15", "value": "https://www.ckplayer.vip/jiexi/?url=" },
    { "label": "线路16", "value": "https://jx.xmflv.com/?url=" },
    { "label": "线路17", "value": "https://jx.aidouer.net/?url=" },
    { "label": "线路18", "value": "https://jx.nnxv.cn/tv.php?url=" },
    { "label": "线路19", "value": "https://www.administratorw.com/video.php?url=" },
    { "label": "线路20", "value": "https://jx.ppflv.com/?url=" },
    { "label": "线路21", "value": "https://vip.wpsseo.cn/jx/?url=" },
    { "label": "线路22", "value": "https://z1.im1907.top/?jx=" },
    { "label": "线路23", "value": "https://jx.dmflv.cc/?url=" },
    { "label": "线路24", "value": "https://jx.xymp4.cc/?url=" },
    { "label": "线路25", "value": "https://jx.77flv.cc/?url=" },
    { "label": "线路26", "value": "https://vip.xxphp.cn/1907.php?eps=&amp;wd=" },
    { "label": "线路27", "value": "https://player.jxok.uno/line.php?n=1&amp;url=" },
    { "label": "线路28", "value": "https://video.isyour.love/player/getplayer?url=" },
    { "label": "线路29", "value": "https://www.yemu.xyz/?url=" },
    { "label": "线路30", "value": "https://www.h8jx.com/jiexi.php?url=" },
    { "label": "线路31", "value": "https://lziplayer.com/?url=" },
    { "label": "线路32", "value": "https://jx.2s0.cn/player/?url=" },
    { "label": "线路33", "value": "https://www.pouyun.com/?url=" },
    { "label": "线路34", "value": "https://jx.2s0.cn/playr/?url=" },
    { "label": "线路35", "value": "https://im1907.top/?jx=" },
]

var pandaBoxParseSites = {
    'm.bilibili.com': {
        selector: '#bilibiliPlayer',
        originUrl: () => location.href,
    },
    'm.iqiyi.com': {
        selector: '.m-video-player-wrap',
        originUrl: () => location.href,
    },
    'm.le.com': {
        selector: '.playB',
        originUrl: () => location.href,
    },
    'm.pptv.com': {
        selector: '.pp-details-video',
        originUrl: () => location.href,
    },
    'm.tv.sohu.com': {
        selector: '.player-view',
        originUrl: () => location.href,
    },
    'm.v.qq.com': {
        selector: '.new-style-player',
        originUrl: () => location.href,
    },
    'www.youku.com': {
        selector: '.callEnd_box',
        originUrl: () => location.href,
    }
}

pandaBoxInitCommon()
function pandaBoxInitCommon() {
    pandaBoxInitSidebar()
    pandaBoxInitHome()
    pandaBoxInitURL()
    pandaBoxInitCountdown()
    pandaBoxInitIframe()
    pandaBoxInitIframeParseList()
}
// 侧边栏
function pandaBoxInitSidebar() {
    // 创建侧边栏
    const sidebarEle = document.createElement('div');
    document.body.appendChild(sidebarEle);
    sidebarEle.id = 'pandaBoxSidebar';
    Object.assign(sidebarEle.style, {
        position: 'fixed',
        left: '-80%',
        top: 0,
        width: '80%',
        height: '100vh',
        background: '#333',
        color: '#fff',
        transition: 'left .3s',
        zIndex: 10000
    });

    // 创建打开按钮
    const toggleEle = document.createElement('button')
    sidebarEle.appendChild(toggleEle)
    toggleEle.textContent = '>'
    Object.assign(toggleEle.style, {
        position: 'absolute',
        top: '70%',
        transform: 'translateY(-50%)',
        right: '-40px',
        width: '40px',
        height: '60px',
        background: '#333',
        color: '#fff',
        border: 'none',
        borderRadius: '0px 5px 5px 0px',
        opacity: 0.9
    });
    toggleEle.onclick = function () {
        const isHidden = sidebarEle.style.left == '-80%'
        sidebarEle.style.left = isHidden ? 0 : '-80%';
        toggleEle.textContent = isHidden ? '<' : '>'
    }
}
function pandaBoxInitHome() {
    const sidebarEle = document.getElementById("pandaBoxSidebar")
    const homeEle = document.createElement('div');
    sidebarEle.appendChild(homeEle)
    Object.assign(homeEle.style, {
        padding: "5px"
    });

    const titleEle = document.createElement('h3');
    homeEle.appendChild(titleEle)
    titleEle.textContent = '返回首页'
    Object.assign(titleEle.style, {
        margin: "0px 0px 5px 0px"
    });

    const buttonEle = document.createElement('button');
    buttonEle.textContent = '返回首页'
    Object.assign(buttonEle.style, {
        marginLeft: "5px",
        padding: "2px 10px"
    });
    homeEle.appendChild(buttonEle)
    buttonEle.onclick = function () {
        const websiteWebview = plus.webview.getWebviewById("websiteWebview");
        if (websiteWebview) {
            websiteWebview.close()
        }
    }
}
// 获取当前地址
function pandaBoxInitURL() {
    const sidebarEle = document.getElementById("pandaBoxSidebar")
    const urlEle = document.createElement('div');
    sidebarEle.appendChild(urlEle)
    Object.assign(urlEle.style, {
        padding: "5px"
    });

    const titleEle = document.createElement('h3');
    urlEle.appendChild(titleEle)
    titleEle.textContent = '当前地址'
    Object.assign(titleEle.style, {
        margin: "0px 0px 5px 0px"
    });

    const inputEle = document.createElement('input');
    urlEle.appendChild(inputEle)
    inputEle.value = location.href
    Object.assign(inputEle.style, {
        marginLeft: "5px",
        background: "#fff",
        color: "#000",
    });

    const buttonEle = document.createElement('button');
    buttonEle.textContent = '复制'
    Object.assign(buttonEle.style, {
        marginLeft: "5px",
    });
    urlEle.appendChild(buttonEle)
    buttonEle.onclick = function () {
        inputEle.select();
        document.execCommand('copy');
        plus.nativeUI.toast('文本已复制！');
    }
}
// 定时功能
function pandaBoxInitCountdown() {
    const sidebarEle = document.getElementById("pandaBoxSidebar")
    const countdownEle = document.createElement('div');
    sidebarEle.appendChild(countdownEle)
    Object.assign(countdownEle.style, {
        padding: "5px"
    });

    const titleEle = document.createElement('h3');
    countdownEle.appendChild(titleEle)
    titleEle.textContent = '定时设置'
    Object.assign(titleEle.style, {
        margin: "0px 0px 5px 0px"
    });

    const secondsEle = document.createElement('span');
    titleEle.appendChild(secondsEle)
    secondsEle.textContent = '【暂无定时】'
    Object.assign(secondsEle.style, {
        marginLeft: "5px"
    });

    const list = [{
        label: '关闭定时',
        value: 0
    }, {
        label: '30分',
        value: 60 * 30
    }, {
        label: '60分',
        value: 60 * 60
    }, {
        label: '90分',
        value: 60 * 90
    }, {
        label: '120分',
        value: 60 * 120
    }, {
        label: '180分',
        value: 60 * 180
    }]


    let timer = null;
    let seconds = 0;
    for (const item of list) {
        const btnEle = document.createElement("button");
        countdownEle.appendChild(btnEle)
        btnEle.textContent = item.label
        Object.assign(btnEle.style, {
            height: "30px",
            width: "80px",
            margin: "0px 5px 5px"
        });
        btnEle.onclick = () => startCountDown(item)
    }
    startCountDown({ value: plus.storage.getItem('pandaBoxStorageCountdown') || 0 })
    function startCountDown(item) {
        clearInterval(timer)
        if (!item.value) {
            secondsEle.textContent = '【暂无定时】'
            plus.storage.removeItem("pandaBoxStorageCountdown");
            return;
        }
        seconds = item.value
        timer = setInterval(() => {
            seconds--
            secondsEle.textContent = `【${Math.floor(seconds / 60)}分${seconds % 60}秒】`
            plus.storage.setItem('pandaBoxStorageCountdown', '' + seconds);
            if (seconds <= 0) {
                clearInterval(timer)
                plus.storage.removeItem("pandaBoxStorageCountdown");
                plus.runtime.quit();
            }
        }, 1000)
    }
}
function pandaBoxInitIframe() {
    const site = pandaBoxParseSites[location.hostname]
    if (!site) {
        return
    }
    const selectorEle = document.querySelector(site.selector)
    if (selectorEle) {
        const iframeEle = document.createElement("iframe")
        iframeEle.setAttribute("pandaBox-src", site.originUrl())
        iframeEle.setAttribute("allowfullscreen", "allowfullscreen")
        iframeEle.id = "pandaBoxIframe"
        Object.assign(iframeEle.style, {
            width: "100%",
            height: "100%",
            border: 'none',
            position: 'absolute',
            background: '#000',
            left: 0,
            top: 0,
            zIndex: 10000
        });
        selectorEle.appendChild(iframeEle)
        let pandaBoxParseDefault = plus.storage.getItem("pandaBoxParseDefault");
        if (pandaBoxParseDefault) {
            pandaBoxParseDefault = JSON.parse(pandaBoxParseDefault)
        }
        iframeEle.contentWindow.location.replace((pandaBoxParseDefault?.value || pandaBoxParseList[1].value) + site.originUrl());
    }
}

function pandaBoxInitIframeParseList() {
     const site = pandaBoxParseSites[location.hostname]
    if (!site) {
        return
    }
    const selectorEle = document.querySelector(site.selector)
    if (!selectorEle) {
        return
    }
    const sidebarEle = document.getElementById("pandaBoxSidebar")
    const parseListEle = document.createElement('div');
    sidebarEle.appendChild(parseListEle)
    Object.assign(parseListEle.style, {
        padding: "5px",
    });

    const titleEle = document.createElement('h3');
    parseListEle.appendChild(titleEle)
    titleEle.textContent = '线路设置'
    Object.assign(titleEle.style, {
        margin: "0px 0px 5px 0px"
    });

    const parseEle = document.createElement('span');
    titleEle.appendChild(parseEle)
    let pandaBoxParseDefault = plus.storage.getItem("pandaBoxParseDefault");
    if (pandaBoxParseDefault) {
        pandaBoxParseDefault = JSON.parse(pandaBoxParseDefault)
    }
    parseEle.textContent = `【${pandaBoxParseDefault?.label || pandaBoxParseList[1].label}】`
    Object.assign(parseEle.style, {
        marginLeft: "5px"
    });

    for (const item of pandaBoxParseList) {
        const btnEle = document.createElement("button");
        parseListEle.appendChild(btnEle)
        btnEle.textContent = item.label
        Object.assign(btnEle.style, {
            height: "30px",
            width: "80px",
            margin: "0px 5px 5px"
        });
        btnEle.onclick = function () {
            const iframeEle = document.getElementById('pandaBoxIframe')
            if (!iframeEle) {
                plus.nativeUI.toast('未检测到视频元素');
                return;
            }
            parseEle.textContent = `【${item.label}】`

            const dataSrc = iframeEle.getAttribute("pandaBox-src")
            if (!dataSrc) {
                plus.nativeUI.toast('未检测到源地址');
                return;
            }
            if (item.value) {
                plus.storage.setItem("pandaBoxParseDefault", JSON.stringify(item));
                iframeEle.contentWindow.location.replace(item.value + dataSrc);
                iframeEle.style.display = 'block'
            } else {
                iframeEle.contentWindow.location.replace('');
                iframeEle.style.display = 'none'
            }
        }
    }

}